package corej.course.thread.multiThread;

public class TestSynchronized {

    public static void main(String[] args) throws Exception {

        MyStack1 ms = new MyStack1();

        ms.print();

        Thread t1 = new PushThread(ms);
        Thread t2 = new PopThread(ms);

        t1.start();
        t2.start();

        Thread.sleep(20000);
        ms.print();
    }
}

class MyStack1 {

    char[] data = {'A', 'B', ' ', ' ', ' ', ' '};
    int index = 2;

    public synchronized void push(char c) {
        System.out.println(c + " pushed!");
        data[index] = c;

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
        }

        index++;
    }

    public synchronized void pop() {
        index--;
        System.out.println(data[index] + " poped!");
        data[index] = ' ';
    }

    public void print() {
        for (int i = 0; i < 6; i++) {
            System.out.print(data[i] + "\t");
        }
        System.out.println();
    }
}

class PushThread extends Thread {
    MyStack1 ms;

    public PushThread(MyStack1 ms) {
        super();
        this.ms = ms;
    }

    public void run() {
        ms.push('C');
    }

}

class PopThread extends Thread {
    MyStack1 ms;

    public PopThread(MyStack1 ms) {
        super();
        this.ms = ms;
    }

    public void run() {
        ms.pop();
    }

}

/*
 * public synchronized void m(){ } public void m(){ synchronized(this){ } }
 */

abstract class T {
    // public synchronized abstract void m();
    public synchronized static void m() {
        // System.out.println(this);
    }
}
